package com.smartcampus.demo.service;


import com.smartcampus.demo.domain.Temperature;
import com.smartcampus.demo.repository.TemperatureDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TemperatureService {

    @Autowired
    private TemperatureDao temperatureDao;

    public void saveTemperature(Temperature temperature) {
        temperature.setTime(LocalDateTime.now());
        temperatureDao.save(temperature);
    }

    public List<Temperature> getAllTemperature() {
        return temperatureDao.findAll();
    }

    public Temperature getTemperatureById(Long id) {
        return temperatureDao.findById(id).orElse(null);
    }

    public List<Temperature> getLatestNTemperature(int n) {
        return temperatureDao.findTopNByOrderByCreatedAtDesc(n);
    }
}
